﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcEngine.Core.Repositories;
using MvcEngine.Core.Utils;
using MvcEngine.Core.Base;
using MvcEngine.Core.Helpers;


namespace MvcEngine.Core.Website
{
    public class SiteManager : BaseManager, ISiteManager
    {
        private readonly ISiteRepository siteRepository = null;
        private readonly ISkinRepository skinRepository = null;
        private readonly ISkinTemplateRepository skinTemplateRepository = null;

        public SiteManager(ISiteRepository siteRepository, ISkinRepository skinRepository, ISkinTemplateRepository skinTemplateRepository)
        {
            this.siteRepository = siteRepository;
            this.skinRepository = skinRepository;
            this.skinTemplateRepository = skinTemplateRepository;
        }

        public static ISiteManager Current
        {
            get { return IoC.Resolve<ISiteManager>(); }
        }

        public Site GetSite(Guid id)
        {
            return CacheManager.Current.FromCache(string.Format(CacheKey.WebsiteKeys.WebsiteId, id), CacheKey.WebsiteKeys.WebsiteCategory,
                () =>
                {
                    var site = siteRepository.Get(id);
                    ConvertXmlToProperties(site);
                    return site;
                });
        }

        public Site GetSiteByDomain(string domain)
        {
            return CacheManager.Current.FromCache(string.Format(CacheKey.WebsiteKeys.WebsiteDomain, domain), CacheKey.WebsiteKeys.WebsiteCategory,
                () =>
                {
                    var site = siteRepository.GetSiteByDomain(domain);
                    ConvertXmlToProperties(site);
                    return site;
                });
        }

        public IList<Site> GetSites()
        {
            return CacheManager.Current.FromCache(CacheKey.WebsiteKeys.WebsiteAll, CacheKey.WebsiteKeys.WebsiteCategory,
                () =>
                {
                    var sites = siteRepository.GetAll();
                    foreach (var item in sites)
                    {
                        ConvertXmlToProperties(item);
                    }
                    return sites;
                });
        }

        public void SaveSite(Site site)
        {
            Guard.ThrowIfEmptyGuid(site.Id);

            using (var unitOfWork = UnitOfWorkHelper.Get())
            {
                var oldItem =  siteRepository.Get(site.Id);
                if (oldItem != null)
                {
                    oldItem.ModifiedDate = DateTime.Now;
                    oldItem.Xml = site.Fields;
                    siteRepository.Update(site);
                }
                else
                {
                    site.Xml = site.Fields;
                    siteRepository.Add(site);
                }

                
                unitOfWork.Commit();
            }
            CacheManager.Current.ClearCache(CacheKey.WebsiteKeys.WebsiteCategory);
        }

        public Skin GetSkin(Guid id)
        {
            return CacheManager.Current.FromCache(string.Format(CacheKey.WebsiteKeys.SkinId, id), CacheKey.WebsiteKeys.SkinCategory,
                () => skinRepository.Get(id));
        }

        public IList<Skin> GetSkins()
        {
            return CacheManager.Current.FromCache(CacheKey.WebsiteKeys.SkinAll, CacheKey.WebsiteKeys.SkinCategory,
                () => skinRepository.GetAll());
        }

        public SkinTemplate GetSkinTemplate(Guid id)
        {
            return CacheManager.Current.FromCache(string.Format(CacheKey.WebsiteKeys.SkinTemplateId, id), CacheKey.WebsiteKeys.SkinTemplateCategory,
                () => skinTemplateRepository.Get(id));
        }

        public IList<SkinTemplate> GetSkinTemlates(Guid skinId)
        {
            return CacheManager.Current.FromCache(string.Format(CacheKey.WebsiteKeys.SkinTemplatesBySkinId, skinId), CacheKey.WebsiteKeys.SkinTemplateCategory,
                () => skinTemplateRepository.GetAll(skinId));
        }

        public SkinTemplate GetDefaultSkinTemplate(Guid skinId)
        {
            return CacheManager.Current.FromCache(string.Format(CacheKey.WebsiteKeys.SkinDefaultTemplateId, skinId), CacheKey.WebsiteKeys.SkinTemplateCategory,
                () => skinTemplateRepository.GetDefault(skinId));
        }
    }
}
